package com.swingson.properties.specificclass_impl;

import com.swingson.Swingson;
import com.swingson.SwingsonGui;
import com.swingson.jsonparser.JsonElement;
import com.swingson.other.ConditionAndAction;
import com.swingson.other.MethodExecutor;
import com.swingson.properties.AbstractAssignMethodPropertyProcessor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JTextField;

/**
 * @author sad
 */
public class ActionComponentPropertyProcessor extends AbstractAssignMethodPropertyProcessor {

    static List<Class[]> argumentsVariants = new ArrayList<Class[]>() {
        {
            add(new Class[]{});
            add(new Class[]{ActionEvent.class});
        }
    };

    @Override
    protected String getName() {
        return "Text field Action listener";
    }

    @Override
    protected List<Class[]> methodArgumentsVariants() {
        return argumentsVariants;
    }

    @Override
    protected void attachMethod(JsonElement jsonElement, final JComponent component, final SwingsonGui gui, String propertyName, final MethodExecutor method, final int methodArgumentVariant, final ConditionAndAction conditionAndAction) {
        if (component instanceof JTextField) {
            ((JTextField) component).addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        if (methodArgumentVariant == 0) {
                            method.invoke();
                        } else {
                            method.invoke(e);
                        }
                        runAction(conditionAndAction, gui, component);
                    } catch (Throwable ex) {
                        onError(gui, ex);
                    }
                }
            });
        } else if (component instanceof AbstractButton) {
            ((AbstractButton) component).addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!Swingson.isEnabledUserInput()) {
                        return;
                    }
                    try {
                        if (methodArgumentVariant == 0) {
                            method.invoke();
                        } else {
                            method.invoke(e);
                        }
                        runAction(conditionAndAction, gui, component);
                    } catch (Throwable ex) {
                        onError(gui, ex);
                    }
                }
            });
        } else if (component instanceof JMenuItem) {
            ((JMenuItem) component).addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!Swingson.isEnabledUserInput()) {
                        return;
                    }
                    try {
                        if (methodArgumentVariant == 0) {
                            method.invoke();
                        } else {
                            method.invoke(e);
                        }
                        runAction(conditionAndAction, gui, component);
                    } catch (Throwable ex) {
                        onError(gui, ex);
                    }
                }
            });
        }
    }
}
